Ubuntu Compression and Decompression: Detailed Explanation of the tar Command

In Ubuntu, `tar` is the core tool for file archiving and compression. It can package multiple files/directories into a `.tar` archive and, when combined with compression tools like `gzip`, `bzip2`, or `xz`, generate compressed formats such as `.tar.gz`, `.tar.bz2`, or `.tar.xz`. The basic syntax is: `tar [options] [archive_name] [files/directories]` Key options include: - `-c`: Create an archive - `-x`: Extract an archive - `-t`: List archive contents - `-v`: Verbose mode (show process) - `-f`: Specify the archive name (must be placed **immediately after** the options) Compression options (`-z` for gzip, `-j` for bzip2, `-J` for xz) must be used with `-c` (create) or `-x` (extract). Common operations: - Pack and compress: `tar -czvf archive.tar.gz file1 file2` - Extract: `tar -xzvf archive.tar.gz` - List contents: `tar -tvf archive.tar.gz` - Extract to a specific directory: Add `-C <path>` (e.g., `tar -xzvf archive.tar.gz -C /target/dir`) **Notes**: - Compression format must match the option (e.g., use `-z` for `.tar.gz`). - When using `-f`, the archive name must follow immediately after the options (e.g., `tar -czvf archive.tar.gz` is correct; `tar -cvf -z archive.tar.gz` is invalid). - Archiving directories preserves their original structure.

Read More